package city.spring.modules.medicalstation.doctorsOrderRecord.service.impl;

import city.spring.modules.medicalstation.doctorsOrderRecord.entity.DoctorsOrderRecordEntity;
import city.spring.modules.medicalstation.doctorsOrderRecord.repository.DoctorsOrderRecordRepository;
import city.spring.modules.medicalstation.doctorsOrderRecord.service.DoctorsOrderRecordService;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static city.spring.utils.UserUtils.getPrincipal;

/**
 * Service：医嘱记录
 *
 * @author 谢金婷
 * @date 2020-11-09 17:04:43
 */
@CacheConfig(cacheNames = {DoctorsOrderRecordServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class DoctorsOrderRecordServiceImpl extends ServiceImpl<DoctorsOrderRecordRepository, DoctorsOrderRecordEntity> implements DoctorsOrderRecordService {
    private final UserService userService;

    public DoctorsOrderRecordServiceImpl(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void saveDoctorsOrderRecord(DoctorsOrderRecordEntity entity) {
        UserEntity userEntity = userService.getUserByIdOrAccount(getPrincipal().getName());
        entity.setStartDoctor(userEntity.getId());
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateDoctorsOrderRecord(DoctorsOrderRecordEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteDoctorsOrderRecord(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteDoctorsOrderRecord(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }
}